//
//  SpecialView.m
//  AmountBody
//
//  Created by smm on 2018/1/6.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "SpecialView.h"
#import "Masonry.h"
#import "EqualCellSpaceFlowLayout.h"
#import "ShoppingCollectionViewCell.h"
#import <ReactiveObjC/ReactiveObjC.h>
#define Back_Color RGBColorAlpha(255, 66, 89, 1)
@implementation SpecialView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
       
    }
    return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        
    }
    return self;
}
-(void)loadUI
{
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = 30;
    CGFloat jianGew = 80;
    CGFloat l_jianGe = 10;
    int count = 5;
    for (int i=0; i<_modelArray.count; i++) {
        UIView * f_view = [self viewWithTag:_View_Tag+i-1];
        UIView * view = [[UIView alloc]init];
        [self addSubview:view];
        [view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.equalTo(@(viewH));
            if (f_view) {
                make.top.equalTo(f_view.mas_bottom).offset(l_jianGe);
                
            }else{
                make.top.equalTo(self).offset(l_jianGe);
            }
        }];
        
        view.tag = _View_Tag+i;
        UILabel * label = [[UILabel alloc]init];
        [view addSubview:label];
        [label mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(view);
            make.height.equalTo(@(viewH));
            make.width.equalTo(@(200));
        }];
        label.text = [_modelArray objectAtIndex:i];
        label.textColor = ZhongZi_Color;
        label.font = Cell_Medium_Font;
        
        for (int j=0; j<count; j++) {
            UIButton * tmpButton = [view viewWithTag:_Button_Tag+i*100+j-1];
            UIButton * button = [[UIButton alloc]init];
            [view addSubview:button];
            [button setTitle:[NSString stringWithFormat:@"%@%d",[_modelArray objectAtIndex:i],j] forState:UIControlStateNormal];
            button.titleLabel.font  =Cell_Little_Font;
            button.tag = _Button_Tag+i*100+j;
            [button sizeToFit];
            //            [button.titleLabel sizeToFit];
            [button setTitleColor:QianZi_color forState:UIControlStateNormal];
            
            [button setBianKuangAndColor:Line_color];
            [button setYuanJiaoForRadius:5.0 andMasks:NO];
            if ((tmpButton.frame.origin.x+tmpButton.frame.size.width+l_jianGe+button.frame.size.width+20)>(viewW)) {
                button.frame = CGRectMake(jianGew, tmpButton.frame.origin.y+tmpButton.frame.size.height+l_jianGe, button.frame.size.width+20, viewH);
            }else{
                if (tmpButton.frame.size.width == 0) {
                    button.frame = CGRectMake(jianGew+tmpButton.frame.origin.x+tmpButton.frame.size.width, tmpButton.frame.origin.y, button.frame.size.width+20, viewH);
                }else{
                    button.frame = CGRectMake(l_jianGe+tmpButton.frame.origin.x+tmpButton.frame.size.width, tmpButton.frame.origin.y, button.frame.size.width+20, viewH);
                }
                
            }
            [button addTarget:self action:@selector(ButtonFunc:) forControlEvents:UIControlEventTouchUpInside];
            
            
        }
        UIButton * f_button = [view viewWithTag:_Button_Tag+i*100];
        [f_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        if (_backColor == nil) {
            f_button.backgroundColor = Back_Color;
        }else{
            f_button.backgroundColor = _backColor;
        }
        
        UIButton * button = [view viewWithTag:_Button_Tag+i*100+count-1];
        view.frame = CGRectMake(0, l_jianGe+f_view.frame.origin.y+f_view.frame.size.height, viewW, button.frame.size.height+button.frame.origin.y);
        [self addSubview:view];
        
    }
    UIView * l_view = [self viewWithTag:_View_Tag+_modelArray.count-1];
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, l_view.frame.origin.y+l_view.frame.size.height+10);
}
-(void)loadUIForMasonry
{
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
//    [self.superview layoutIfNeeded]; 
//    CGFloat viewW = self.frame.size.width;
    CGFloat viewH = 30;
    CGFloat jianGew = 80;
    CGFloat l_jianGe = 10;
    int count = 8;
    for (int i=0; i<_modelArray.count; i++) {
        UIView * f_view = [self viewWithTag:_View_Tag+i-1];
        UIView * view = [[UIView alloc]init];
        [self addSubview:view];
        [view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self).offset(-20);
            if (f_view) {
                make.top.equalTo(f_view.mas_bottom).offset(l_jianGe);
                
            }else{
                make.top.equalTo(self).offset(l_jianGe);
            }
        }];
        
        view.tag = _View_Tag+i;
        UILabel * label = [[UILabel alloc]init];
        [view addSubview:label];
        [label mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(view);
            make.height.equalTo(@(viewH));
            make.width.equalTo(@(200));
        }];
        label.text = [_modelArray objectAtIndex:i];
        label.textColor = ZhongZi_Color;
        label.font = Cell_Medium_Font;
        UIButton * tmpButton = nil;
        for (int j=0; j<count; j++) {
            UIButton * button = [[UIButton alloc]init];
            [view addSubview:button];
            [button setTitle:[NSString stringWithFormat:@"%@%d",[_modelArray objectAtIndex:i],j] forState:UIControlStateNormal];
            button.titleLabel.font  =Cell_Little_Font;
            button.tag = _Button_Tag+i*100+j;
            [button sizeToFit];
            //            [button.titleLabel sizeToFit];
            [button setTitleColor:QianZi_color forState:UIControlStateNormal];
            
            [button setBianKuangAndColor:Line_color];
            [button setYuanJiaoForRadius:5.0 andMasks:NO];
            __block int bigWidth = 0;
            layoutViews(^{
                bigWidth = 480;
            }, ^{
                bigWidth = 440;
            });
            if ((tmpButton.frame.origin.x+tmpButton.frame.size.width+l_jianGe+button.frame.size.width+20)>(bigWidth)) {
                button.frame = CGRectMake(jianGew, tmpButton.frame.origin.y+tmpButton.frame.size.height+l_jianGe, button.frame.size.width+20, viewH);
            }else{
                if (tmpButton.frame.size.width == 0) {
                    button.frame = CGRectMake(jianGew+tmpButton.frame.origin.x+tmpButton.frame.size.width, tmpButton.frame.origin.y, button.frame.size.width+20, viewH);
                }else{
                    button.frame = CGRectMake(l_jianGe+tmpButton.frame.origin.x+tmpButton.frame.size.width, tmpButton.frame.origin.y, button.frame.size.width+20, viewH);
                }

            }
            [button addTarget:self action:@selector(ButtonFunc:) forControlEvents:UIControlEventTouchUpInside];
            tmpButton = button;
            
        }
        UIButton * f_button = [view viewWithTag:_Button_Tag+i*100];
        [f_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        if (_backColor == nil) {
            f_button.backgroundColor = Back_Color;
        }else{
            f_button.backgroundColor = _backColor;
        }
        
        [view mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(tmpButton.frame.size.height+tmpButton.frame.origin.y));
        }];
        
        
    }
    UIView * l_view = [self viewWithTag:_View_Tag+_modelArray.count-1];
    [l_view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-20);
    }];
}
-(CGFloat)viewH
{
    return self.frame.size.height;
}


-(void)ButtonFunc:(UIButton*)button
{
    int tag = (int)button.tag-_Button_Tag;
    int index = tag/100;
    for (int i=0; i<8; i++) {
        UIButton * tmpButton = [self viewWithTag:_Button_Tag+index*100+i];
        tmpButton.backgroundColor = [UIColor whiteColor];
        [tmpButton setTitleColor:QianZi_color forState:UIControlStateNormal];
    }
    if (_backColor == nil) {
        button.backgroundColor = Back_Color;
    }else{
        button.backgroundColor = _backColor;
    }
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
